package cn.edu.nju.software.cdiy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.Supplier;
import cn.edu.nju.software.cdiy.dao.SupplierDao;

public class SupplierDaoImpl implements SupplierDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Supplier login(String signInName, String password) {
		Session session = sessionFactory.getCurrentSession();

		Supplier supplier = null;
		try {

			Query query = session
					.createQuery("from Supplier s where s.name=:sname and s.password=:spassword");
			query.setString("sname", signInName);
			query.setString("spassword", password);
			supplier = (Supplier) query.uniqueResult();

		} catch (Exception e) {

			e.printStackTrace();
		} 
		return supplier;

	}

	@Override
	public Supplier updateSupplier(Supplier supplier) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
			
			session.update(supplier);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
		return supplier;
	}

	@Override
	public Supplier updateRegistor(Supplier supplier) {
		if (checkExist(supplier)) {
			return null;
		} else {
			Session session = sessionFactory.getCurrentSession();
			

			try {
				
				session.save(supplier);
			
			} catch (Exception e) {
				
				e.printStackTrace();
			} 
			return supplier;

		}
	}

	private boolean checkExist(Supplier supplier) {

		Session session = sessionFactory.getCurrentSession();

		
		boolean returnBoolean = false;
		try {

		
			Query query = session
					.createQuery("from Supplier u where u.name=:suppliername");

			query.setString("suppliername", supplier.getName());
			List list = query.list();

			if (list.size() != 0) {

				returnBoolean = true;
			}
		
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
		return returnBoolean;
	}

	@Override
	public List<Supplier> getAllSupplier() {
		Session session = sessionFactory.getCurrentSession();
		List<Supplier> list = null;

		try {
			Query query = session.createQuery("from Supplier");
			list = query.list();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean deleteSupplierById(Long id) {
		Session session = sessionFactory.getCurrentSession();
	
		boolean returnBoolean = false;
		try {
		
			// Supplier s=session.load(Supplier.class,
			Query query = session
					.createQuery("delete from Supplier s where s.id=:id");
			query.setLong("id", id);
			query.executeUpdate();
			
			returnBoolean = true;
		} catch (Exception e) {
			
			e.printStackTrace();
			returnBoolean = false;
		} 
		
		return returnBoolean;
	}

	@Override
	public List<Hardware> getSupplierBySupplierId(long id, String sql) {
		Session session = sessionFactory.getCurrentSession();
		
		List<Hardware> returnHardware = null;
		try {
		
			// Supplier s=session.load(Supplier.class,
			Query query = session
					.createQuery(sql);
			query.setLong("id", id);
			returnHardware = query.list();
		

		} catch (Exception e) {
		
			e.printStackTrace();

		} 
		return returnHardware;
	}

	

}
